/*
 * CProject.java
 *
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 *
 * This file is part of JRogueDPS.
 *
 * JRogueDPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JRogueDPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JRogueDPS.  If not, see <http://www.gnu.org/licenses/>.
 */

package project;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jroguedps.CGlobal;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import utility.CUtility;

/**
 *
 * @author Mani
 */
public class CProject {
    private String m_name;
    private String m_realm;
    private String m_region;
    private double m_savedDPS;

    public void read() {
        String l_string = CUtility.openXMLFileOpenDialog("characters", "Choose a character data file");
        if (l_string == null) {
            return;
        }

        this.read(l_string);
    }

    public void read(String l_fileName) {
        File l_xml = new File(l_fileName);
        SAXReader l_reader = new SAXReader();
        Document l_doc;
        try {
            l_doc = l_reader.read(l_xml);
            Element l_element = (Element) l_doc.selectSingleNode("//root/character");
            this.parseXMLElement(l_element);
            l_element = (Element) l_doc.selectSingleNode("//root/paperdoll");
            CGlobal.g_paperDoll.parseXMLElement(l_element);
            l_element = (Element) l_doc.selectSingleNode("//root/raidbuffs");
            CGlobal.g_raidBuffs.parseXMLElement(l_element);
            l_element = (Element) l_doc.selectSingleNode("//root/talents");
            CGlobal.g_talents.parseXMLElement(l_element);
            l_element = (Element) l_doc.selectSingleNode("//root/playstyle");
            CGlobal.g_playStyle.parseXMLElement(l_element);
            l_element = (Element) l_doc.selectSingleNode("//root/hat");
            CGlobal.g_HAT.parseXMLElement(l_element);
            CGlobal.g_rootFrame.setStats();
        } catch (DocumentException ex) {
            Logger.getLogger(CProject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void parseXMLElement(Element p_element) {
        CGlobal.g_rootFrame.playerNameTextField.setText(p_element.attributeValue("name"));
        CGlobal.g_rootFrame.realmTextField.setText(p_element.attributeValue("realm"));
        CGlobal.g_rootFrame.regionComboBox.setSelectedItem(p_element.attributeValue("region"));
        CGlobal.g_savedDps = Double.parseDouble(p_element.attributeValue("saveddps"));
    }
    
    public void save() {
        String l_string = CUtility.openXMLFileSaveDialog("characters", "Set a filename for this character build");
        if (l_string == null) {
            return;
        }

        this.save(l_string);
    }

    public void save(String p_fileName) {
        Document doc = DocumentFactory.getInstance().createDocument();
        Element root = doc.addElement("root");
        this.addXMLData(root);
        CGlobal.g_paperDoll.addXMLData(root);
        CGlobal.g_raidBuffs.addXMLData(root);
        CGlobal.g_talents.addXMLData(root);
        CGlobal.g_playStyle.addXMLData(root);
        CGlobal.g_HAT.addXMLData(root);

        if (!p_fileName.endsWith(".xml")) {
            p_fileName = p_fileName + ".xml";
        }

        FileOutputStream fos;
        try {
            fos = new FileOutputStream(p_fileName);
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer;
            try {
                writer = new XMLWriter(fos, format);
                try {
                    writer.write(doc);
                    writer.flush();
                    writer.close();
                } catch (IOException ex) {
                    Logger.getLogger(CProject.class.getName()).log(Level.SEVERE, null, ex);
                }

            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CProject.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(CProject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addXMLData(Element p_root) {
        p_root.addElement("character").
                    addAttribute("name", CGlobal.g_rootFrame.playerNameTextField.getText()).
                    addAttribute("realm", CGlobal.g_rootFrame.realmTextField.getText()).
                    addAttribute("region", CGlobal.g_rootFrame.regionComboBox.getSelectedItem().toString()).
                    addAttribute("saveddps", Double.toString(CGlobal.g_savedDps));
    }
}
